const query = require('@/modules/database')

module.exports.count = () => {
  let _sql = 'SELECT count(id) as count FROM category';
  return query(_sql)
}

module.exports.list = (req) => {
  let _sql = `SELECT * FROM category WHERE title LIKE '%${req.params.title || ''}%'`;
  return query(_sql)
}

module.exports.detail = (req) => {
  let _sql = `SELECT * FROM category WHERE id=${req.query.id}`;
  return query(_sql)
}

module.exports.insert = (req) => {
  const params = req.body;
  let _sql = `INSERT INTO category SET title=${params.title}`
  if (params.desc) _sql += `,desc=${params.desc}`
  if (params.cover) _sql += `,cover=${params.cover}`
  if (params.icon) _sql += `,icon=${params.icon}`
  if (params.num) _sql += `,num=${params.num}`
  if (params.status) _sql += `,status=${params.status}`
  if (params.level) _sql += `,level=${params.level}`
  if (params.remark) _sql += `,remark=${params.remark}`
  return query(_sql)
}

module.exports.update = (req) => {
  const params = req.body;
  let _sql = `UPDATE category SET title=${params.title}`
  if (params.desc) _sql += `,desc=${params.desc}`
  if (params.cover) _sql += `,cover=${params.cover}`
  if (params.icon) _sql += `,icon=${params.icon}`
  if (params.num) _sql += `,num=${params.num}`
  if (params.status) _sql += `,status=${params.status}`
  if (params.level) _sql += `,level=${params.level}`
  if (params.remark) _sql += `,remark=${params.remark}`
  _sql += `WHERE id=${params.id}`
  return query(_sql)
}

module.exports.remove = (req) => {
  let _sql = `DELETE FROM category WHERE id = ${req.body.id}`
  return query(_sql)
}